//
//  FansViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "FansViewController.h"
#import "AttentionCell.h"
@interface FansViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSMutableArray *dataArr;//数据
@property (nonatomic,assign)NSInteger pageNumber;//页码
@end

@implementation FansViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [self getFansList];
}

-(void)creatView
{
    [self initTitleWithTitle:@"我的粉丝"];
    _dataArr = [NSMutableArray new];
    [self.view addSubview:self.tableV];
}

-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 60*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //加载cell
    AttentionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AttentionCell"];
    if (cell == nil) {
        cell = [[AttentionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AttentionCell"];
    }
    [cell setDataDic:_dataArr[indexPath.row]];
    if ([_dataArr[indexPath.row][@"type"] integerValue] == 2) {
        cell.GuanZLab.text = @"关注";
    }
    __weak typeof(self) weakSelf = self;
    cell.blcok = ^(){
        [weakSelf followPostWithUser:weakSelf.dataArr[indexPath.row]];
    };
    return cell;
}

-(void)getFansList
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/user/fansList" Dic:@{@"id":[Storage getToken]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [_dataArr addObjectsFromArray:responseDic[@"data"]];
                    [_tableV reloadData];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
                if ([_tableV.mj_header isRefreshing]) {
                    [_tableV.mj_header endRefreshing];
                }
                if ([_tableV.mj_footer isRefreshing]) {
                    [_tableV.mj_footer endRefreshing];
                }
            });
        }];
    });
}

-(void)followPostWithUser:(NSDictionary *)user
{
    NSString *url = @"/user/addFollow";
    if ([user[@"type"] integerValue] == 1) {
        url = @"/user/cancelFollow";
    }
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:url Dic:@{@"othersUser.id":user[@"othersUserId"],@"ownUser.id":[Storage getToken]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    NSString *msg = @"关注成功";
                    if ([user[@"type"] integerValue] == 1) {
                        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:user];
                        [dic setObject:@"2" forKey:@"type"];
                        [_dataArr replaceObjectAtIndex:[_dataArr indexOfObject:user] withObject:dic];
                        msg = @"取关成功";
                    }else{
                        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:user];
                        [dic setObject:@"1" forKey:@"type"];
                        [_dataArr replaceObjectAtIndex:[_dataArr indexOfObject:user] withObject:dic];
                    }
                    [_tableV reloadData];
                    [TomamaUI creatAlertWithTarget:self message:msg timeInterval:2];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

@end
